package pers.liu.yibandemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import pers.liu.yibandemo.entity.ChatLog;
import pers.liu.yibandemo.mapper.ChatLogMapper;

import java.util.List;

@Service
public class ChatLogService extends ServiceImpl<ChatLogMapper, ChatLog> {

    @Autowired
    private ChatLogMapper chatLogMapper;

    // 添加聊天记录
    public void addChatLog(ChatLog chatLog) {
        chatLogMapper.insert(chatLog);
    }

    // 查询所有聊天记录
    public List<ChatLog> getAllChatLogs() {
        return chatLogMapper.selectList(null);
    }

    // 用户查询聊天记录（用户和管理员发送的消息）
    public List<ChatLog> getChatLogs(String userId) {
        QueryWrapper<ChatLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                        .eq("sender_id", userId)
                        .eq("sender_type", "parent")
                        .or()
                        .eq("receiver_id", userId)
                        .eq("receiver_type", "parent"))
                .orderByAsc("timestamp"); // 按照 timestamp 字段升序排序

        return chatLogMapper.selectList(queryWrapper);
    }
}
